package cn.xiaoxiang.org.service.impl;

import cn.xiaoxiang.basic.config.BussinessException;
import cn.xiaoxiang.basic.util.BaiduAuditUtils;
import cn.xiaoxiang.basic.util.MailUtil;
import cn.xiaoxiang.org.domain.Employee;
import cn.xiaoxiang.org.domain.Shop;
import cn.xiaoxiang.org.domain.ShopAuditLog;
import cn.xiaoxiang.org.dto.ShopAuditDto;
import cn.xiaoxiang.org.dto.ShopSettlementDto;
import cn.xiaoxiang.org.mapper.EmployeeMapper;
import cn.xiaoxiang.org.mapper.ShopAuditLogMapper;
import cn.xiaoxiang.org.mapper.ShopMapper;
import cn.xiaoxiang.org.service.EmployeeService;
import cn.xiaoxiang.org.service.IShopService;
import cn.xiaoxiang.basic.service.impl.BaseServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxiang
 * @since 2022-11-20
 */
@Service
@Slf4j
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    // 注入邮件对象
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(ShopSettlementDto vo) {
        //1.数据是否没有
        //(1)我们使用注解

        this.one(vo);
        this.two(vo);
    }
//审核通过
    @Override
    public void auditPass(ShopAuditDto dto) {
        //更改状态
        Shop shop = new Shop();
        //
        shop.setId(dto.getShopId());
        //魔法值
        shop.setState(1);
        //修改状态
        shopMapper.update(shop);
        //添加日志
        ShopAuditLog auditLog = new ShopAuditLog();

        //todo
        auditLog.setAuditId(1);
        auditLog.setAuditTime(new Date());
        auditLog.setShopId(dto.getShopId());
        auditLog.setNote(dto.getNote());
        shopAuditLogMapper.add(auditLog);
        //发送消息
        MailUtil.getMaiUtil("792130298@qq.com","792130298@qq.com","<h1>尊敬的用户，你已通过了我们宠物之家审核标准，点击下方点解可激活</h1>"+
                "http://localhost:8080/shop/active/"+dto.getShopId());
       /* try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("792130298@qq.com");
            helper.setSubject("宠物乐园中心");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>尊敬的用户，你已通过了我们宠物之家审核标准，点击下方点解可激活</h1>"+
                    "http://localhost:8080/shop/active/"+dto.getShopId(),true);
            // 4.添加附件
            helper.addAttachment("好东西.jpg",new File("C:\\Users\\86157\\Pictures\\学习资料\\94712881_233298307882871_8867165738998894151_n.jpg"));
            // 5.设置收件人
            helper.setTo("792130298@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            log.info("发送失败，请重新发送",e);
//            e.printStackTrace();
        }*/
    }
//激活
    @Override
    public void active(Long id) {
        Shop shop = shopMapper.findById(id);
        shop.setId(id);
        shop.setState(3);
        shopMapper.update(shop);
    }

    //驳回
    @Override
    public void auditReject(ShopAuditDto dto) {
        //更改状态
        Shop shop = new Shop();
        //
        shop.setId(dto.getShopId());
        //魔法值
        shop.setState(2);
        //修改状态
        shopMapper.update(shop);
        //添加日志
        ShopAuditLog auditLog = new ShopAuditLog();
        auditLog.setAuditId(2);
        auditLog.setAuditTime(new Date());
        auditLog.setShopId(dto.getShopId());
        auditLog.setNote(dto.getNote());
        shopAuditLogMapper.add(auditLog);
        //发送消息
        MailUtil.getMaiUtil("792130298@qq.com","792130298@qq.com","<h1>尊敬的用户，你未能通过了我们宠物之家审核标准，点击下方连接重新激活</h1>"+
                "http://localhost:8081/#/registerAgain/?shopId="+dto.getShopId());
     /*   try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("792130298@qq.com");
            helper.setSubject("宠物乐园中心");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>尊敬的用户，你未能通过了我们宠物之家审核标准，点击下方连接重新激活</h1>"+
                    "http://localhost:8081/#/registerAgain/?shopId="+dto.getShopId(),true);
            // 4.添加附件
            helper.addAttachment("好东西.jpg",new File("C:\\Users\\86157\\Pictures\\学习资料\\117645480_758957951569900_7699270236926985110_n.jpg"));
            // 5.设置收件人
            helper.setTo("792130298@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            log.info("发送失败，请重新发送",e);
//            e.printStackTrace();
        }*/

    }

    @Override
    public void settlementagain(Shop shop) {
        //更改状态
        Shop shop1 = new Shop();
        //
        shop1.setId(shop.getId());
        shop1.setName(shop.getName());
        shop1.setTel(shop.getTel());
        shop1.setAddress(shop.getAddress());
        shop1.setLogo(shop.getLogo());

        //魔法值
        shop1.setState(0);
        //修改状态
        shopMapper.update(shop1);
    }


    private void two(ShopSettlementDto vo) {
        //5.保存数据
        //保存店铺的数据
        Shop shops = vo.getShop();
        shopMapper.add(shops);
        //保存个人信息
        Employee emp = vo.getEmp();
        emp.setShopId(shops.getId());
        employeeMapper.add(emp);
        //店长Id
        Shop updateShop = new Shop();
        updateShop.setId(shops.getId());
        updateShop.setAdminId(emp.getId());
        shopMapper.update(updateShop);
    }

    private void one(ShopSettlementDto vo) {
        //2.两次密码是否一致
        Employee admin = vo.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BussinessException("两次密码必须一致");
        }

        //3.是否已经有着家店
        //根据电话号码来查找数据
        Shop shop= shopMapper.findByPhone(vo.getTel());
        if (null!=shop){
            throw new BussinessException("当前店铺已经存在");
        }
        //4.管理员是否已经有自己的店铺
        //(1)根据姓名先查出是在哪一家店铺里的人
        Employee employee=employeeMapper.findByName(admin.getUsername());
        if (null!=employee&&null!=employee.getShopId()){

            //（2）.在查处是不是这个店里的管理员,根据这个人的Id是否员工表里管路员Id一致
            shop =shopMapper.findByAdminId(employee.getId());
            if (null!=shop&&shop.getAdminId().equals(employee.getId())){
                throw new BussinessException("你已经有自己的店铺了，不能再开分店了");
            }
        }
        if (!BaiduAuditUtils.TextCensor(vo.getName())){
            throw new BussinessException("店铺名称非法！");
        }
        // 审核 店铺logo
        if (!BaiduAuditUtils.ImgCensor(vo.getLogo())){
            throw new BussinessException("店铺logo非法！");
        }
    }


}
